package TeacherAdmin;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;

import java.util.Set;

/*
    修改密码场景
 */
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ModifyPassword  extends Init {
    @ParameterizedTest
    @CsvFileSource(resources = "../test.csv")//要有对应的文件test.csv
    @Order(1)
    void login(String name,String password) throws InterruptedException {
        System.out.println("username:"+name);
        System.out.println("password:"+password);
        //输入账号密码
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(1) > div > div > input")).sendKeys(name);
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(2) > div > div > input")).sendKeys(password);
        //点击登录
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(3) > div > button")).click();
        webDriver.manage().window().maximize();
        sleep(3000);

        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://192.168.63.194:10001/digitrain/login/toIndex#/home",curUrl);
        sleep(3000);
    }

    @ParameterizedTest
    @CsvFileSource(resources = "../modify.csv")
    @Order(2)
    void modify(String newPassword,String repeatPassword) throws InterruptedException {
        //点击修改密码
        webDriver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/ul/li[1]")).click();
        sleep(3000);
         //输入新的密码
        webDriver.findElement(By.xpath("/html/body/div[3]/div/div/div/form/div[1]/div[2]/div[1]/div/input")).sendKeys(newPassword);
        //输入重复密码
        webDriver.findElement(By.xpath("/html/body/div[3]/div/div/div/form/div[2]/div[2]/div/div/input")).sendKeys(repeatPassword);
        //点击确定
        webDriver.findElement(By.xpath("/html/body/div[3]/div/div/footer/button[2]/span")).click();
        //获取跳转页URL
        Set<String> handles = webDriver.getWindowHandles();
        String curHandle = "";
        for (String x:
            handles ) {
            curHandle = x;
        }
        webDriver.switchTo().window(curHandle);
        String curUrl = webDriver.getCurrentUrl();
        System.out.println("curURL:"+curUrl);
        //判断
        Assertions.assertEquals("http://192.168.63.194:10001/digitrain/login/doLogout",curUrl);
    }
}
